﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccess;
using System.Data;
using Framework;
using DataAccess.CustomerDAC;
using Model.Custome;

namespace DomMgmtSystem.View
{
    /// <summary>
    /// GetHSBaseInfo 的摘要说明
    /// </summary>
    public class GetBaseInfo : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            ///type:1出库时航摄 2出库时遥感 3介质管理
            string type = context.Request["type"];
            string sqdh = context.Request["CSQDH"];
            if (type.IsNullOrEmpty() || sqdh.IsNullOrEmpty())
                return;

            string result = "";
            string nr = "";
            int sjl = 0;
            if (type == "1")
            {
                Model.TB_HSYXRK_BASE model = TB_HSYXRK_BASE.GetModel(sqdh);
                if (model != null)
                {
                    DataSet ds = TB_HSYXRK_HD.GetList("NSQID=" + model.NID);
                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        nr += item["CNR"];
                        if (nr.IsNotEmpty())
                            nr += ";";
                        sjl += Convert.ToInt32(item["FSJL"].ToString());
                    }
                    Model.Custome.HSInData hsdata = new Model.Custome.HSInData()
                    {
                        CNR = nr,
                        FMJ = 0,
                        FSJL = sjl,
                        NHPS = 0
                    };
                    result = JsonHelper.DataToStrJSON(hsdata);
                }
            }
            else if (type == "2")
            {
                Model.TB_YGYXRK_BASE model = CustomerDAC.GetModel(sqdh);
                if (model != null)
                {
                    DataSet ds = TB_YGYXRK_HD.GetList(string.Format("NRWID='{0}'", model.NID));
                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        nr += item["CFW"];
                        if (nr.IsNotEmpty())
                            nr += ";";
                        sjl += Convert.ToInt32(item["FSJL"].ToString());

                    }
                    InYGData ygdata = new InYGData()
                    {
                        CNR = nr,
                        FSJL = sjl,
                        CWXLX = model.CWXLX,
                        FMJ = model.FMJ,
                        NJS = model.NJS
                    };
                    result = JsonHelper.DataToStrJSON(ygdata);
                }
            }
            else
            {
                List<HDData> list = new List<HDData>();
                DataSet ds = TB_HSYXRK_HD.GetListFromView("CSQDH=" + sqdh);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        list.Add(new HDData()
                        {
                            JZBH = item["NJZBH"].ToString()
                        });
                    }

                }
                else
                {
                    DataSet ds1 = TB_YGYXRK_HD.GetListFromView("CRWDH=" + sqdh);
                    if (ds1 != null && ds1.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow item in ds1.Tables[0].Rows)
                        {
                            list.Add(new HDData()
                            {
                                JZBH = item["NJZBH"].ToString()
                            });
                        }

                    }

                }
                result = JsonHelper.DataToStrJSON(list);
            }

            context.Response.ContentType = "text/json";
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}